﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Text;
namespace SanalKlavye
{
    public enum KlavyeTipi
    {
        Tam,
        Numerik,
        AlfaNumerik

    }
    public class Klavye : Panel
    {
        KlavyeTipi tip;
        public Klavye()
            : base()
        {
            Klvye(this);
            this.SizeChanged += new EventHandler(Klavye_SizeChanged);
            this.Tuslandi += new EventHandler<ButonaBasildi>(Klavye_Basildi);
            tip = KlavyeTipi.Tam;
            Tipi = KlavyeTipi.Tam;
        }

        Color tusrenk;

        public Color TusRengi
        {
            get
            {

                return this.Controls[0].BackColor;
            }
            set
            {
                tusrenk = value;
                foreach (Control item in this.Controls)
                {
                    item.BackColor = value;
                }
            }
        }


        public KlavyeTipi Tipi
        {
            get
            {
                return tip;
            }
            set
            {
                tip = value;
                this.Controls.Clear();
                Klvye(this);
            }
        }
        public FlatStyle btntipi = FlatStyle.Standard;

        public FlatStyle TusSitili
        {
            get
            {
                Button btn = this.Controls[0] as Button;
                return btn.FlatStyle;
            }
            set
            {
                foreach (Control item in this.Controls)
                {
                    Button btn = item as Button;
                    btntipi = value;
                    btn.FlatStyle = btntipi;

                }
            }
        }

        void Klavye_Basildi(object sender, ButonaBasildi e)
        {
            krk = e.Message;
        }



        void Klavye_SizeChanged(object sender, EventArgs e)
        {

            this.Controls.Clear();
            Klvye(this);
            TusRengi = tusrenk;
        }


        private void Klvye(Panel flwklavye)
        {
            int boy = (this.Height) / 4;
            int en = (this.Width) / 13;
            int sira = 0;
            int yukseklik = 0;
            int kat = 0;
            int tipegoresirala = (en * 10) + 5;




            switch (tip)
            {
                case KlavyeTipi.Tam:
                    tipegoresirala = (en * 10) + 5;
                    en = (this.Width) / 13;
                    HarfDiz(flwklavye, boy, en, ref sira, ref yukseklik, ref kat);
                    yukseklik = 0;
                    sira = 0;

                    RakamDiz(flwklavye, boy, en, ref sira, ref yukseklik, tipegoresirala);

                    break;
                case KlavyeTipi.Numerik:
                    tipegoresirala = 0;
                    en = (this.Width) / 3;
                    yukseklik = 0;
                    sira = 0;

                    RakamDiz(flwklavye, boy, en, ref sira, ref yukseklik, tipegoresirala);

                    break;
                case KlavyeTipi.AlfaNumerik:
                    en = (this.Width) / 10;
                    HarfDiz(flwklavye, boy, en, ref sira, ref yukseklik, ref kat);
                    break;
            }

            float fl = 2;
            if (this.Controls[0].Height < this.Controls[0].Width)
            {
                fl = this.Controls[0].Height / 2;
            }
            else
            {
                fl = this.Controls[0].Width / 2;
            }
            if (fl <= 0) { fl = 2; }
            this.Font = new Font(this.Font.FontFamily, fl);

        }

        private void HarfDiz(Panel flwklavye, int boy, int en, ref int sira, ref int yukseklik, ref int kat)
        {
            for (int i = 0; i < 27; i++)
            {
                Button btn = new Button();
                btn.Height = boy;
                btn.Width = en;
                btn.FlatStyle = btntipi;

                switch (i)
                {
                    case 0:
                        btn.Text = "Q";
                        break;
                    case 1:
                        btn.Text = "W";
                        break;
                    case 2:
                        btn.Text = "E";
                        break;
                    case 3:
                        btn.Text = "R";
                        break;
                    case 4:
                        btn.Text = "T";
                        break;
                    case 5:
                        btn.Text = "Y";
                        break;
                    case 6:
                        btn.Text = "U";
                        break;
                    case 7:
                        btn.Text = "I";
                        break;
                    case 8:
                        btn.Text = "O";
                        break;
                    case 9:
                        btn.Text = "P";
                        break;
                    case 10:
                        btn.Text = "A";
                        break;
                    case 11:
                        btn.Text = "S";
                        break;
                    case 12:
                        btn.Text = "D";
                        break;
                    case 13:
                        btn.Text = "F";
                        break;
                    case 14:
                        btn.Text = "G";
                        break;
                    case 15:
                        btn.Text = "H";
                        break;
                    case 16:
                        btn.Text = "J";
                        break;
                    case 17:
                        btn.Text = "K";
                        break;
                    case 18:
                        btn.Text = "L";
                        break;
                    case 19:
                        btn.Text = "Z";
                        break;
                    case 20:
                        btn.Text = "X";
                        break;
                    case 21:
                        btn.Text = "C";
                        break;
                    case 22:
                        btn.Text = "V";
                        break;
                    case 23:
                        btn.Text = "B";
                        break;
                    case 24:
                        btn.Text = "N";
                        break;
                    case 25:
                        btn.Text = "M";
                        break;
                    default:
                        break;
                }


                if (i == 26)
                {

                    btn.Text = "SPACE";
                    yukseklik++;
                    sira = 0;
                    kat += en / 2;
                    btn.Width = en * 6;
                }
                flwklavye.Controls.Add(btn);
                btn.Click += new EventHandler(btn_Click);

                if (sira >= 10)
                {
                    yukseklik++;
                    sira = 0;
                    kat += en / 2;
                }

                if (kat > 0 && sira >= 9)
                {
                    yukseklik++;
                    sira = 0;
                    kat += en / 2;
                }
                btn.Left = (en * sira) + kat;
                btn.Top = boy * yukseklik;
                if (btn.Top + btn.Height > flwklavye.Height)
                {
                    flwklavye.Height = btn.Top + btn.Height;
                }

                sira++;


            }
        }

        private void RakamDiz(Panel flwklavye, int boy, int en, ref int sira, ref int yukseklik, int tipegoresirala)
        {
            for (int i = 0; i < 11; i++)
            {
                Button btn = new Button();
                btn.Height = boy;
                btn.Width = en;
                btn.FlatStyle = btntipi;

                if (i < 3)
                {
                    btn.Text = (7 + i).ToString();

                }
                else if (i > 5 && i < 9)
                {
                    btn.Text = (i - 5).ToString();

                }
                else if (i == 9)
                {
                    btn.Width = en * 2;
                    btn.Text = "0";
                }
                else
                {
                    btn.Text = (i + 1).ToString();
                }
                flwklavye.Controls.Add(btn);
                btn.Click += new EventHandler(btn_Click);

                if (sira >= 3)
                {
                    yukseklik++;
                    sira = 0;
                }
                btn.Left = (en * sira) + tipegoresirala;
                btn.Top = boy * yukseklik;
                if (i == 10)
                {
                    btn.Left += en;
                    btn.Text = "<<";
                }

                if (btn.Top + btn.Height > flwklavye.Height)
                {
                    flwklavye.Height = btn.Top + btn.Height;
                }

                sira++;
            }
        }


        public string krk = "";
        public void btn_Click(object sender, EventArgs e)
        {
            Button btn = sender as Button;
            this.DemoEvent(btn.Text);
        }
        public string harf
        {
            get
            {
                if (krk == "SPACE")
                {
                    krk = " ";
                }
                if (krk == "<<")
                {
                    krk = "@";
                }
                return krk;
            }

        }
        public event EventHandler<ButonaBasildi> Tuslandi;

        public void DemoEvent(string val)
        {
            // Copy to a temporary variable to be thread-safe.
            EventHandler<ButonaBasildi> temp = Tuslandi;
            if (temp != null)
                temp(this, new ButonaBasildi(val));
        }
    }
    public class ButonaBasildi : EventArgs
    {
        private string msg;

        public ButonaBasildi(string messageData)
        {
            msg = messageData;
        }
        public string Message
        {
            get { return msg; }
        }
    }
}
